Android studio button Button Four Methods for Binding Events [Instance Code]

  • 2021-10-13 08:46:11
  • OfStack

Button is a very simple control in Android, which is very common in our usual projects and has a high utilization rate. The following is an example code to introduce four methods of binding events for Android studio button buttons. The specific code is as follows:


package com.geli_2.sujie.sujiegeili2testbutton;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
  private Button mBtClick;
  private Button mBtClick2;
  private Button mBtClick3;
  private Button mBtClick4;
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mBtClick = (Button) findViewById(R.id.btnTest1);      //1 , fdv  Find button id
                                  // 2 ,  Alt Ctrl F  Form class global Field
    mBtClick.setOnClickListener(new View.OnClickListener()   //3 Type this line of code to automatically embed the inner class method 
    {
      @Override
      public void onClick(View v)
      {                            //4 ,  Toast tab tab  Bring out toast  Method   The above is the method 1
        Toast.makeText(MainActivity.this, "hello world", Toast.LENGTH_SHORT).show();
      }
    });
    mBtClick2 = (Button) findViewById(R.id.btnSample2);     //5 , find out button 2  Adj.  button id  fdv
    mBtClick2.setOnClickListener(new Button_2_OnClickListener()); //9 The instantiation method is given to button  Binding   These are the methods 2
    mBtClick3 = (Button) findViewById(R.id.btnTry3);      //10 , find out button 3  Adj.  button id  fdv
    mBtClick3.setOnClickListener(this);             //11 ,  this  Denote  main acitivity
                                  //12  But in this case, the method parameter is onclicklistener Interface, here  this Alt Enter  The implemented interface is added to this method 
    mBtClick4 = (Button) findViewById(R.id.btnExample4);    //5 , find out button 4  Adj.  button id  fdv
  }
  /**
   * Called when a view has been clicked.
   *
   * @param v The view that was clicked.
   */
  @Override
  public void onClick(View v)
  {
    // Method 3
  }
  class Button_2_OnClickListener implements View.OnClickListener //6 , setting 1 A  click  Method   After being named, it inherits from  view
                                  // onclicklistener
                                  //7 ,   Afterward   Inheritance  implements  Upper  alt + enter  To implement the method 
  {
    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v)
    {
      // Method 2
    }
  }
  public void click(View view)
  {
    // Method 4
  }
}

Summarize


Related articles: